/**
 * 
 */
app.controller('resInfoController', ['$scope', '$uibModal', '$window', 'resCRUDService', 'filterFilter', 'pagingFilter', function($scope, $uibModal, $window, resCRUDService, filterFilter, pagingFilter) {
	
	$scope.ttreses = [];	
	$scope.reses = [];
	
	$scope.currPage = 1;
	$scope.totalItems;
	$scope.itemsPer = 15;
	
	
	if($window.innerHeight < 800) {
		$scope.itemsPer = 10;
	}
	
	resCRUDService.resGet().then(function(result) {
		console.log(result);
		$scope.ttreses = result;
		var ttoppos = filterFilter($scope.ttreses, '');
		pagingFilter.listLoad(ttoppos, $scope.itemsPer);
		$scope.totalItems = pagingFilter.getPgInfo().resArylength;
		$scope.reses = [];
		$scope.reses = pagingFilter.getCurrentPage($scope.currPage);
	}, function() {
		
	});
	
	$scope.$watch('currPage', function(newValue, oldValue) {
		$scope.reses = [];
		$scope.reses = pagingFilter.getCurrentPage(newValue);
	});
	
	$scope.doAdd = function() {
		var uibModal = $uibModal.open({
			 animation: false,
			 templateUrl: './views/resAdd.html',
			 size: 'lg',
			 controller: ['$scope', '$uibModalInstance', 'resCRUDService', function($scope, $uibModalInstance, resCRUDService) {
				 $scope.res = {
							resourceId: 0,
							username: null,
							capability: null,
							designation: null,
							dateOfJoin: null,
							kinId: null,
							name: null,
							chineseName: null,
							primarySkill: null,
							status: '1'
						}
				 
				 $scope.save = function() {
					 var keys = Object.keys($scope.res);
					 var isNull = false;
					 
					 for(var i = 0; i < keys.length; i++) {
						 if($scope.res[keys[i]] == null) {
							 isNull = true;
						 }
					 }
					 
					 if(!isNull) {
						 resCRUDService.resInsert($scope.res).then(function() {
							 $uibModalInstance.close();
						 }, function() {
							 
						 }) 
					 }
				 }
				 
			 }],
			 resolve: {
				 
			 }
		  });
		
		uibModal.result.then(function(value) {
			resCRUDService.resGet().then(function(result) {
				console.log(result);
				$scope.ttreses = result;
				var ttoppos = filterFilter($scope.ttreses, '');
				pagingFilter.listLoad(ttoppos, $scope.itemsPer);
				$scope.totalItems = pagingFilter.getPgInfo().resArylength;
				$scope.reses = [];
				$scope.reses = pagingFilter.getCurrentPage($scope.currPage);
			}, function() {
				
			});
		});
		
	}
	
	$scope.doEdit = function(resId) {
		var uibModal = $uibModal.open({
			 animation: false,
			 templateUrl: './views/resAdd.html',
			 size: 'lg',
			 controller: ['$scope', '$uibModalInstance', 'resourceDetailService', 'resCRUDService', 'resourceId', function($scope, $uibModalInstance, resourceDetailService, resCRUDService, resourceId) {
				 console.log(resourceId);
				 resourceDetailService(Number.parseInt(resourceId)).then(function(result) {
					 $scope.res = result;
				 })
				 
				 $scope.save = function() {
					 resCRUDService.resUpdate($scope.res).then(function() {
						 $uibModalInstance.close();
					 }, function() {
						 
					 })
				 }
			 }],
			 resolve: {
				 resourceId: function() {
					 return resId;
				 }
			 }
		  });
		  
		  uibModal.result.then(function(value) {
			  resCRUDService.resGet().then(function(result) {
					console.log(result);
					$scope.ttreses = result;
					var ttoppos = filterFilter($scope.ttreses, '');
					pagingFilter.listLoad(ttoppos, $scope.itemsPer);
					$scope.totalItems = pagingFilter.getPgInfo().resArylength;
					$scope.reses = [];
					$scope.reses = pagingFilter.getCurrentPage($scope.currPage);
				}, function() {
					
				});
		  })
	}
	
}])